package com.huarui.quotation.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;
import org.ewaf.framework.util.HttpUtil;
import com.huarui.inventory.model.Inventory;
import com.huarui.inventory.service.InventoryService;
import com.huarui.params.Params;
import com.huarui.quotation.model.Quotation;
import com.huarui.quotation.service.QuotationService;
import com.huarui.saleReturn.util.ConstantParamEnum;
import com.huarui.user.model.SessionMessage;
import com.huarui.util.FuncUtil;

public class QuotationAction extends BaseAction {

	private static final long serialVersionUID = -8901380466823216280L;
	private static final Logger log = Logger.getLogger(QuotationAction.class);
	private QuotationService quotationService;
	private InventoryService inventoryService;
	private String dir;
	private String sort;
	private Integer start;
	private Integer limit;
	private SessionMessage sessionMessage;
	private Map<String, Object> quotaMap;
    private String queryParam;
    @SuppressWarnings("unchecked")
	private ListModel listModel;
    
//    private Integer companyId;
//
//	public Integer getCompanyId() {
//		return companyId;
//	}
//
//	public void setCompanyId(Integer companyId) {
//		this.companyId = companyId;
//	}

//	@SuppressWarnings("unchecked")
//	public String getQuotationsByComplex() throws IOException {
////		HttpServletRequest resquest = HttpUtil.getRequest();
////
////		Map<String, String[]> map = resquest.getParameterMap();
////
////		Map<String, Object> pm = FuncUtil.convertToParamMap(map);
//		
//		Map<String, Object> pm = new HashMap<String, Object>();
//		pm.put("companyId", companyId);
//		//排除 （不公开）
//		pm.put("publicanLevel", Quotation.PUBLIC_LEVEL_NO);
//		
//		Integer _start = (start == null) ? 0 : start;
//		pm.put("start", _start);
//		pm.put("pageSize", Params.PAGE_SIZE);
//
//		quotaMap = quotationService.getQuotationListByComplex(pm);
//		return Params.QUOTA_SHOW_LIST_MESS;
//	}

	@SuppressWarnings("unused")
	private JSONArray getJsonFromList(List<Map<String, String>> list) {
		JSONArray ja = JSONArray.fromObject(list);
		return ja;
	}

	public String index() {

		return "index";
	}

	/**
	 * xzz
	 * 
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public String queryQuotation() throws IOException {
		
		quotaMap = new HashMap();
		if(queryParam != null){
			JSONObject queryObject = JSONObject.fromObject(queryParam);
			quotaMap = (Map) JSONObject.toBean(queryObject, Map.class);	
		}
		listModel = ListModelParamUtil();
		ListModel<Map> inventoryLitModel = inventoryService.getInvetoryForMapByConditions(quotaMap,listModel);
		quotaMap.clear();
		quotaMap.put(ConstantParamEnum.TOTALPROPERTY.getType(), inventoryLitModel.getTotal());
		quotaMap.put(ConstantParamEnum.ROOT.getType(), inventoryLitModel.getResult());
		return "json";
	}
	
	@SuppressWarnings("unchecked")
	private ListModel ListModelParamUtil() {
		listModel = new ListModel();
		listModel.setPage(start ==null && limit == null ? 1: start / limit + 1);
		listModel.setPageSize(limit == null ? Integer.valueOf(ConstantParamEnum.PAGESIZE.getType()) : limit);
		listModel.setOrderBy(sort);
		if (dir != null){
			listModel.setDesc(dir.equals(ConstantParamEnum.DESC.getType()) ? true : false);
		}else{
			listModel.setDesc(true);
		}	
		return listModel;
	}
	
	/**
	 * xzz
	 * 
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public String update() throws IOException {
		JSONArray inventoryJson = JSONArray.fromObject(queryParam);
		quotaMap = new HashMap();
		if (inventoryJson.size() > 0) {
			List<Inventory> inventoryList = (List<Inventory>) JSONArray.toCollection(inventoryJson, Inventory.class);
			try {
				quotationService.updateAll(inventoryList);
				quotaMap.put("success", "OK");
			} catch (Exception e) {			
				log.error("报价列表保存更新失败......", e);
				quotaMap.put("failure", "报价列表保存更新失败......");
			}
		}
		return "json";
	}

	public String getQueryParam() {
		return queryParam;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public QuotationService getQuotationService() {
		return quotationService;
	}

	public void setQuotationService(QuotationService quotationService) {
		this.quotationService = quotationService;
	}

	public InventoryService getInventoryService() {
		return inventoryService;
	}

	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public SessionMessage getSessionMessage() {
		return sessionMessage;
	}

	public void setSessionMessage(SessionMessage sessionMessage) {
		this.sessionMessage = sessionMessage;
	}

	public Map<String, Object> getQuotaMap() {
		return quotaMap;
	}

	public void setQuotaMap(Map<String, Object> quotaMap) {
		this.quotaMap = quotaMap;
	}

	@SuppressWarnings("unchecked")
	public ListModel getListModel() {
		return listModel;
	}

	@SuppressWarnings("unchecked")
	public void setListModel(ListModel listModel) {
		this.listModel = listModel;
	}

}
